package com.example.pkmedical.Activity.weight.options;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.SPUtils;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.text.DecimalFormat;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.example.pkmedical.Utils.NumberUtil.byteToInteger;

public class OptionAddContinueActivity extends BaseActivity {

    @BindView(R.id.add_continue_button_ok)
    Button addContinueButtonOk;
    @BindView(R.id.add_continue_button_cancle)
    Button addContinueButtonCancle;
    private static final String TAG = "yangshuo";
    byte[] mBytes;
    String five;
    double mNowWeight, mLastWeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_optionaddcontinue);
        ButterKnife.bind(this);
    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.WEIGHT_SHOW_ADD) {
            Log.d(TAG, "onDataReceivedADD [ byte[] ]: " + Arrays.toString(event.getBytes()));
            mBytes = event.getBytes();
            distinguish(mBytes);
        } else {
            Log.d(TAG, "onDataReceivedADD [ byte[] ]: NoThisData");
        }
    }

    /**
     * 返回数据识别
     */
    private void distinguish(byte[] bytes) {

        if (DistinguishUtil.distinguishUtil(bytes)) {
            switch (bytes[3]) {
                case 5:
                    int aaa;
                    int bbb;
                    aaa = byteToInteger(bytes[4]);
                    bbb = byteToInteger(bytes[5]);
                    five = aaa + "." +bbb;
                    break;
            }
        } else {
        }
        SPUtils.getInstance().put("user_difference_weight_two", five);
        SPUtils.getInstance().put("user_now_weight",five);
        DecimalFormat df = new DecimalFormat("#.00");
        double oneWeight = Double.parseDouble(SPUtils.getInstance().getString("user_difference_weight_one"));
        double twoWeight = Double.parseDouble(SPUtils.getInstance().getString("user_difference_weight_two"));
        double Difference;
        Log.d("yangshuoDifferent", oneWeight + "-----------" + twoWeight);
        if (twoWeight > oneWeight) {
            CommandUtil.ADD_PLUS_TYPE = 1;
            Difference = twoWeight - oneWeight;
            SPUtils.getInstance().put("user_difference_weight_all", Difference + "");
        } else {
            CommandUtil.ADD_PLUS_TYPE = 0;
            SPUtils.getInstance().put("user_difference_weight_all", "0");
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(MyApp.getAppContext(), OptionChangeOkActivity.class));
                OptionAddContinueActivity.this.finish();
            }
        });

    }


    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.add_continue_button_ok, R.id.add_continue_button_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.add_continue_button_ok:
                SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.GET_WEIGHT_ADD);
//                startActivity(new Intent(MyApp.getAppContext(), OptionChangeOkActivity.class));
//                OptionAddContinueActivity.this.finish();
                break;
            case R.id.add_continue_button_cancle:
                finish();
                break;
        }
    }
}
